package com.wntms.auser.web;

import com.wntms.auser.pojo.Menu;
import com.wntms.auser.service.MenuService;
import com.wntms.auser.service.UserService;
import com.wntms.auser.pojo.User;
import com.wntms.ft.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("/menus")
    public List<Menu> menu(){
        return menuService.getMenusByUser();
    }

    /**
     * 登录方法
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String token = userService.login(user);

        return Result.success(token,"登录成功");

    }

    /**
     * 判断注册时，用户名是否重复
     * @param username
     * @return
     */
    @PostMapping("/inspect")
    public Result inspectUsername(String username){
        boolean flag = userService.inspectUsername(username);
        if (flag == false) {
            return Result.error("用户名重复");
        }else {
            return Result.success(null,null);
        }
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        System.out.println(user);
        boolean register = userService.register(user);
        if (register) {
           return Result.success(null,"注册成功");
        }
        return Result.error("注册失败");
    }

}
